/*
* @Author: root
* @Date:   2017-11-08 10:47:48
* @Last Modified by:   root
* @Last Modified time: 2017-11-08 18:37:57
*/
'user strict'
require('./index.css');
require('page/common/nav-simple/index.js');
var _user  = require('service/user-service.js');
var _utils = require('util/util.js');
//表单的错误提示
var formError = {
	show : function(errMsg){
		$('.error-item').show().find('.err-msg').text(errMsg);
	},
	hide : function(){
		$('.error-item').hide().find('.err-msg').text('');
	}
}
//逻辑
var page = {
	init : function(){
		this.bindEvent();
	},
	bindEvent : function(){
		var _this = this;
		//验证username
		$('#username').blur(function(){
			var username = $.trim($(this).val());
			//如果用户名为空则不验证
			if(!username){
				return;
			}
			// 异步验证用吗是否存在
			_user.checkUsername(username,function(res){
				formError.hide();
			},function(errMsg){
				formError.show(errMsg);
			});
		});
		//注册按钮的点击
		$('#submit').click(function(){
			_this.submit();
		});
		//如果按下回车,也进行提交
		$('.user-content').keyup(function(e){
			//当keyCode等于回车13时候则表示，回车也提交
			if(e.keyCode === 13){
				_this.submit();
			}
		});

	},//提交表单
	submit : function(){
		var formData = {
			username 			:  $.trim($('#username').val()),
			password 			:  $.trim($('#password').val()),
			passwordConfirm 	:  $.trim($('#password-confirm').val()),
			phone 				:  $.trim($('#phone').val()),
			email 				:  $.trim($('#email').val()),
			question 			:  $.trim($('#question').val()),
			answer 				:  $.trim($('#answer').val())
		},
		//表单验证结果
		validateResult = this.formValidate(formData);
		if (validateResult.status) {
			//提交成功
			_user.register(formData,function(res){
				window.location.href ='./result.html?type=register';
			}, function(errMsg){
				formError.show(errMsg);	
			});
			//验证失败，错误提示
		}else {
				formError.show(validateResult.msg);	
		}
	},
	//表单字段的验证
	formValidate : function(formData){
		var result = {
			status : false,
			msg    : ''
		};
		if (!_utils.validate(formData.username,'require')) {
			result.msg = '用户名不能为空';
			return result;
		}
		if (!_utils.validate(formData.password,'require')) {
			result.msg = '密码不能为空';
			return result;
		}
		if (formData.password.length < 6) {
			result.msg = '密码长度不能少于6位';
			return result;
		}
		//验证两次
		if (formData.password!== formData.passwordConfirm) {
			result.msg = '两次输入的密码不一致';
			return result;
		}
		if(!_utils.validate(formData.email, 'email')){
            result.msg = '邮箱格式不正确';
            return result;
        }
		if (!_utils.validate(formData.phone,'phone')) {
			result.msg = '手机格式不正确';
			return result;
		}
		if (!_utils.validate(formData.question,'require')) {
			result.msg = '问题不能为空';
			return result;
		}
		if (!_utils.validate(formData.answer,'require')) {
			result.msg = '答案不能为空';
			return result;
		}
		//通过验证，返回正确提示
		result.status = true;
		result.msg    = '验证通过';
		return result;
	}
};
$(function(){
	page.init();
})
